package com.mall.web.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;

import com.mall.common.util.DateUtil;
import com.mall.pojo.Mall;


public class StringUtil {
	
	public static String hiddenName(String str) {
		String newReturn = null;
		if (StringUtils.isBlank(str)) {
			return str;
		}
		if (str.length() == 1) {
			return newReturn = str + "***";
		}
		if (str.length() == 2) {
			String sub = str.substring(1, str.length());
			newReturn = str.replace(sub, "***");
		}
		if (str.length() >= 3) {// 如果截取开始长度大于字符串的长度的话。则原样返回
			String sub = str.substring(1, str.length() - 1);
			newReturn = str.replace(sub, "***");

		}
		return newReturn;

	}

	public static String hiddenPhone(String str) {
		String newReturn = null;
		if (StringUtils.isBlank(str)) {
			return str;
		}
		newReturn = str.substring(0, str.length() - (str.substring(3)).length()) + "****" + str.substring(7);
		return newReturn;
	}

	public static String hiddenMessage(String str, int begin, int end) {
		if (StringUtils.isBlank(str)) {
			return str;
		}

		if (str.length() < begin) {// 如果截取开始长度大于字符串的长度的话。则原样返回
			return str;
		}

		if (str.length() < end) {// 如果截取结束长度大于字符串的长度的话。取字符串的长度
			end = str.length();
		}

		String sub = str.substring(begin, str.length() - end);
		String newReturn = str.replace(sub, "*****");

		return newReturn;

	}

	/**
	 * 店铺名称截取前8位
	 * 
	 * @param str
	 * @return
	 */
	public String getMallName(String str) {
		String newReturn = null;
		if (StringUtils.isBlank(str)) {
			return str;
		}
		if (str.length() > 8) {
			String sub = str.substring(0, 7);
			newReturn = sub + "...";
		} else {
			newReturn = str;
		}

		return newReturn;
	}

	/**
	 * 截取
	 * 
	 * @param str
	 *            源数据
	 * @param no
	 *            截取的位置
	 * @param addString
	 *            截取后加的字符串
	 * @return
	 */
	public String getSubStringName(String str, Integer no, String addString) {
		String newReturn = null;
		if (StringUtils.isBlank(str)) {
			return str;
		}
		if (str.length() > no) {
			String sub = str.substring(0, no - 1);
			newReturn = sub + addString;
		} else {
			newReturn = str;
		}

		return newReturn;
	}

	public static String getYear(String str) {
		if (StringUtils.isBlank(str)) {
			return str;
		}
		String first = str.substring(0, 1);
		Integer yeFrirst = Integer.parseInt(first);
		if (yeFrirst == 0) {
			return str.substring(1, str.length());
		}
		return str;
	}

	/**
	 * 判断是否是以endString结尾 1：true 0：false
	 * 
	 * @param str
	 * @param endString
	 * @return
	 */
	public static int getBooleanStringEnd(String str, String endString) {
		if (StringUtils.isBlank(str)) {
			return 0;
		}
		if (StringUtils.isBlank(endString)) {
			return 0;
		}
		if (str.endsWith(endString)) {
			return 1;
		}
		return 0;
	}

	// public static void main(String[] args) {
	// System.out.println(getBooleanStringEnd("http://1u49179g10.iask.in:8081/data/",
	// "/data/"));
	// }

	public static boolean isNotBlank(String a) {
		return StringUtils.isNotBlank(a);
	}
	public static boolean isNotBlankLong(Long a) {
		if (a == null || a<0) {
			return false;	
		}else if(a>0){
			return true;
		}
		return false;
		
	}


	public static boolean isBlank(String a) {
		return StringUtils.isBlank(a);
	}

	public static String getPicPath(Long userId) {
		return userId % 128 + "";
	}

	public static Double getZhekou(Integer num) {

		Double intD = null;
		if (num == null || num <= 0) {
			return null;
		}
		intD = (num.doubleValue()) / 10;
		return intD;
	}

	public static String getDuoBaoPriceShow(String date) {
		Date duobaoTime = DateUtil.stringToDate(date, "yyyy-MM-dd HH:mm:ss:SS");
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(duobaoTime);
		int minute = calendar.get(Calendar.MINUTE);// 分
		int hour = calendar.get(Calendar.HOUR);// 小时
		int second = calendar.get(Calendar.SECOND);// 秒
		int mill = calendar.get(Calendar.MILLISECOND);// 秒
		String time = hour + "" + minute + "" + second + "" + mill;
		return time;
	}

	public static String getContentByURLDecoder(String content) {
		try {
			return URLDecoder.decode(content, "UTF-8");
		} catch (UnsupportedEncodingException e) {
		}
		return "";
	}

	public static Long getRoundByYear(String year) {
		Long result = null;
		try {
			if (year.equals(null)) {
				return null;
			}
			result = Math.round(Double.parseDouble(year) / 10) * 10;
		} catch (Exception e) {
			result = 0l;
		}

		return result;
	}

	/**
	 * 页面上显示内容，如果有用户名直接展示用户名，如果没有，则显示电话号码
	 * 
	 * @param phone
	 * @param name
	 * @return
	 */
	public static String getPageShowName(String phone, String name) {
		if (StringUtils.isNotBlank(name)) {
			return name;
		}
		return hiddenMessage(phone, 3, 4);
	}

	public static void main(String[] args) {
		String phone = "士大夫区";
		String[] sd = phone.split("区");
		System.out.println(sd.length);
	}

	

	/**
	 * 去除中文
	 * @param strVarMboxRead
	 * @return
	 */
	public static String removeChina(String strVarMboxRead) {
		if (StringUtils.isBlank(strVarMboxRead)) {
			return "";
		}
		
		String str_OneStr = "";
		String str_Result = "";
		for (int z = 0; z < strVarMboxRead.length(); z++) {
			str_OneStr = strVarMboxRead.substring(z, z + 1);
			if (!str_OneStr.matches("[\u4e00-\u9fa5]+")) {
				if (str_OneStr.matches("[\\x00-\\x7F]+")) {
					str_Result = str_Result + str_OneStr;
				}
			}
		}
		
		return str_Result;
	}
	
	/**
	 * 保留英文和数字
	 * @param str
	 * @return
	 */
	public static String retainEnglishAndNumber(String str){
		if (StringUtils.isBlank(str)) {
			return "";
		}
		return str = str.replaceAll("[^a-z^A-Z^0-9]", "");  
	}
	
	
	
	//网友评分
			public static String getListNetPingfenShow(Integer pingfen){
				String pingfenStr = null;
				if(pingfen != null && pingfen > 0 && pingfen <=5){
					pingfenStr = "网友评分"+pingfen+"星";
				}
				return pingfenStr;
			}
			//政府评分
			public static String getListGovPingfenShow(Mall mall){
				String pingfenStr = null;
				if(mall != null && mall.getMallCategoryId() > 0 && mall.getMallSecCategoryId() > 0){
					if(mall.getMallCategoryId() == 1 && mall.getPingtaiPingFen()> 0){
						switch(mall.getPingtaiPingFen()){
						case 5:
							pingfenStr = "卫生评级5星";
							break;
						case 4:
							pingfenStr = "卫生评级4星";
							break;
						case 3:
							pingfenStr = "卫生评级3星";
							break;
						default:
							pingfenStr = "";
							break;
						}
					}else if(mall.getMallCategoryId() == 101 || (mall.getMallCategoryId()==7 && mall.getMallSecCategoryId() == 101)){
						switch(mall.getPingtaiPingFen()){
						case 5:
							pingfenStr = "国家AAAAA级景区";
							break;
						case 4:
							pingfenStr = "国家AAAA级景区";
							break;
						case 3:
							pingfenStr = "国家AAA级景区";
							break;
						default:
							pingfenStr = "";
							break;
						}
					}
					
					else if(mall.getMallCategoryId() == 42 || (mall.getMallCategoryId()==2 && mall.getMallSecCategoryId() == 42)){
						switch(mall.getPingtaiPingFen()){
						case 5:
							pingfenStr = "卫生评级5星";
							break;
						case 4:
							pingfenStr = "卫生评级4星";
							break;
						case 3:
							pingfenStr = "卫生评级3星";
							break;
						default:
							pingfenStr = "";
							break;
						}
					}else {
						switch(mall.getPingtaiPingFen()){
						case 5:
							pingfenStr = "运营评分5星";
							break;
						case 4:
							pingfenStr = "运营评分4星";
							break;
						case 3:
							pingfenStr = "运营评分3星";
							break;
						default:
							pingfenStr = "";
							break;
						}
					}
					
					
					
				}else {
					switch(mall.getPingtaiPingFen()){
					case 5:
						pingfenStr = "运营评分5星";
						break;
					case 4:
						pingfenStr = "运营评分4星";
						break;
					case 3:
						pingfenStr = "运营评分3星";
						break;
					default:
						pingfenStr = "";
						break;
					}
				}
				return pingfenStr;
			}
			
		//上传的图片路径截取后面的真实图片名
		public String getSufName(String fullName){
			String sufName = null;
			if(fullName == null || fullName.equals("")){
				return null;
			}
			if(fullName.split("-").length > 1){
				sufName = fullName.split("-")[1];
			}
			return sufName;
		}
		
		
		//分割省市区和具体地址
		public static String getPartAddress(String address,int index){
			if(StringUtils.isBlank(address) || !address.contains("区") || index > 1){
				return null;
			}
			String[] partAddress = address.split("区");
			if(index > partAddress.length){
				return null;
			}
			if(index == 0){
				return partAddress[index]+"区";
			}
			
			return partAddress[index];
		}
		public static int booleanIsContain(String attribute,String containStr){
			if(StringUtils.isBlank(attribute) || StringUtils.isBlank(containStr)){
				return 0;
			}
			if (attribute.contains(containStr)) {
				return 1;
			}
		
			return 0;
		}

		public static String subStr(String str){
			String result =null;
			if (StringUtils.isBlank(str)) {
				return null;
			}
			if (str.length()>8) {
				result = str.substring(8, str.length());
			}
			
			return result;
			
		}
}


